package com.lys.eduservice.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.commonutils.JwtUtils;
import com.lys.commonutils.R;
import com.lys.eduservice.client.UcenterClient;
import com.lys.eduservice.entity.EduComment;
import com.lys.eduservice.entity.EduCourse;
import com.lys.eduservice.entity.frontVo.UcenterMember;
import com.lys.eduservice.service.EduCommentService;
import com.lys.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author lys
 * @since 2022-07-18
 */
@Slf4j
@RestController
@CrossOrigin
@Api(tags = "课程评论管理")
@RequestMapping("/eduservice/eduComment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;
    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 根据课程id分页显示评论
     * @param page
     * @param limit
     * @param courseId
     * @return
     */
    @GetMapping("/queryCommentByPage/{page}/{limit}")
    @ApiOperation(value = "评论分页列表")
    public R queryCommentByPage(@PathVariable long page, @PathVariable long limit, String courseId){
        Page<EduComment> pageInfo = new Page<>(page,limit);
        LambdaQueryWrapper<EduComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduComment::getCourseId,courseId);
        queryWrapper.orderByDesc(EduComment::getGmtCreate);
        eduCommentService.page(pageInfo,queryWrapper);

        //将数据封装map
        List<EduComment> eduCommentList = pageInfo.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("items",eduCommentList);
        map.put("current", pageInfo.getCurrent());
        map.put("pages", pageInfo.getPages());
        map.put("size", pageInfo.getSize());
        map.put("total", pageInfo.getTotal());
        map.put("hasNext", pageInfo.hasNext());
        map.put("hasPrevious", pageInfo.hasPrevious());

        return R.ok().data(map);
    }

    //添加评论
    @PostMapping("/auth/save")
    public R addComment(@RequestBody EduComment eduComment, HttpServletRequest request){
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            throw new GuliException(20001,"请先登录");
        }
        //根据memberId获取用户信息
        UcenterMember userInfo = ucenterClient.getInfo(memberId);
        if (userInfo==null){
            throw new GuliException(20001,"添加失败");
        }
        eduComment.setMemberId(memberId);
        eduComment.setNickname(userInfo.getNickname());
        eduComment.setAvatar(userInfo.getAvatar());
        eduCommentService.save(eduComment);

        return R.ok();
    }


}

